# Example for using the Coincidence Counters with python + quTAG
#
# Author: qutools GmbH
# Last edited: Oct 2019
#
# Tested with python 3.7.3 (32bit), numpy-1.13.3 and Windows 7 (64bit)
#
# This is demo code. Use at your own risk. No warranties.
#
# It may be used and modified with no restriction; raw copies as well as 
# modified versions may be distributed without limitation.

# for sleep
import datetime
import time
import os
import numpy as np
import shutil
# This code shows how to get timestamps from a quTAG connected via USB and write them into a text file.

# Import the python wrapper which wraps the DLL functions.
# The wrapper should be in the same directory like this code in the folder '..\QUTAG-V1.x.x\userlib\src'.
try:
        import QuTAG
except:
        print("Time Tagger wrapper QuTAG.py is not in the search path.")


# Initialize the quTAG device
qutag = QuTAG.QuTAG()
index = np.arange(1,11,1)
print('Time start: ', datetime.datetime.now())
#path = r'C:\Users\InQlab_QEN\Desktop\TDC\quTAG-Software_Python-examples-20200604\DataRecording\50uw'
path = r'C:\Users\InQlab_QEN\Desktop\qutag-GUI\DataRecording'
pathtrue=os.path.exists(path)
if pathtrue:
        b=1
        shutil.rmtree(path)
        os.mkdir(path)
else:
        os.mkdir(path)
for i in index:
        nowtime = datetime.datetime.now()
        measuretime=1
        #print(nowtime)
        time.sleep(1)
        #filename = r'C:\Users\InQlab_QEN\Desktop\TDC\quTAG-Software_Python-examples-20200604\DataRecording\DataRecording'+str(measuretime)+'-'+str(i)+'.txt'
        filename = path+'\DataRecording' + str(measuretime) + '-' + str(i) + '.txt'

        # start writing Timestamps from the quTAG
       # print(filename)
        # with open(path.join(r'C:\Users\InQlab_QEN\Desktop\TDC\quTAG-Software_Python-examples-20200604', filename), "w") as f:
        open(filename, 'w')
        # open(os.path.join(args.save_path, "{}.lst".format(args.set)))
        qutag.writeTimestamps(filename, qutag.FILEFORMAT_ASCII)

        # Give some time to accumulate data
        time.sleep(measuretime)  # 1 second sleep time

        # stop writing Timestamps
        qutag.writeTimestamps('', qutag.FILEFORMAT_NONE)
print("Time stop: ",datetime.datetime.now())
# The next function starts or stops writing the timestamp values to a file continuously.
# The timestamps written are already corrected by the detector delays, see example 'qutag-GetHistogramLoop-channelDelay-example.py'.
# Timestamps come in base units of 1 ps. The channel numbers start with 0 in binary formats, with 1 in ASCII.
# A channel number of (100 + Marker Number) is associated with marker input events.
# The 104 is a millisecond tick.
# The following file formats are available:
#    ASCII: FILEFORMAT_ASCII - Timestamp values (int base units) and channel numbers as decimal values in two comma separated columns. Channel numbers range from 1 to 8 in this format.
#    binary: FILEFORMAT_BINARY - A binary header of 40 bytes, records of 10 bytes, 8 bytes for the timestamp, 2 for the channel number, stored in little endian (Intel) byte order.
#    compressed: FILEFORMAT_COMPRESSED - A binary header of 40 bytes, records of 40 bits (5 bytes), 37 bits for the timestamp, 3 for the channel number, stored in little endian (Intel) byte order. No marker events and timer ticks are stored.
#    raw: FILEFORMAT_RAW - Like binary, but without header. Provided for backward compatiblity.






#print("Let's have a look into the file " + filename)



# Disconnects a connected device and stops the internal event loop.
qutag.deInitialize()


